#!/bin/bash

# n8n-nodes-gemini-thinking 发布脚本

echo "🚀 开始发布 n8n-nodes-gemini-thinking..."

# 检查是否已登录 npm
echo "📋 检查 npm 登录状态..."
if ! npm whoami > /dev/null 2>&1; then
    echo "❌ 未登录 npm，请先运行: npm login"
    exit 1
fi

echo "✅ npm 登录状态正常，当前用户: $(npm whoami)"

# 运行预发布检查
echo "🔍 运行预发布检查..."
if ! npm run prepublishOnly; then
    echo "❌ 预发布检查失败，请修复错误后重试"
    exit 1
fi

echo "✅ 预发布检查通过"

# 检查包名是否可用
echo "📦 检查包名可用性..."
if npm view n8n-nodes-gemini-thinking > /dev/null 2>&1; then
    echo "⚠️  包名已存在，将发布新版本"
else
    echo "✅ 包名可用，准备首次发布"
fi

# 显示当前版本
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "📌 当前版本: $CURRENT_VERSION"

# 询问是否继续发布
echo "❓ 确认发布到 npm? (y/N)"
read -r CONFIRM

if [[ $CONFIRM != "y" && $CONFIRM != "Y" ]]; then
    echo "❌ 发布已取消"
    exit 0
fi

# 执行发布
echo "🚀 正在发布..."
if npm publish; then
    echo "🎉 发布成功！"
    echo "📦 包地址: https://www.npmjs.com/package/n8n-nodes-gemini-thinking"
    echo "💡 用户可以通过以下方式安装:"
    echo "   npm install n8n-nodes-gemini-thinking"
    echo "   或在 n8n 界面中搜索: n8n-nodes-gemini-thinking"
else
    echo "❌ 发布失败，请检查错误信息"
    exit 1
fi 